import 'package:flutter/material.dart';
import 'package:fluwx/fluwx.dart';
import 'package:mall/constant/string.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mall/model/user_info.dart';
import 'package:mall/utils/http_util.dart';
import 'package:mall/utils/wechat_userinfo_model.dart';
import 'package:mall/widgets/mall_icon.dart';
import 'package:mall/service/user_service.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:mall/entity/user_entity.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:mall/utils/navigator_util.dart';
import 'package:mall/event/login_event.dart';
import 'package:mall/utils/shared_preferences_util.dart';
import 'package:mall/utils/weixin.dart';

class LoginView extends StatefulWidget {
  @override
  _LoginViewState createState() => _LoginViewState();
}

class _LoginViewState extends State<LoginView> {
  TextEditingController _accountTextControl = TextEditingController();
  TextEditingController _passwordTextControl = TextEditingController();
  UserService userService = UserService();
  UserEntity userEntity;
  bool _autovalidator = false;
  final registerFormKey = GlobalKey<FormState>();
  var _result;

  ///微信登录
  var _result2 = "无";

  @override
  void initState() {
    // _initFluwx();
    super.initState();
    // fluwx.weChatResponseEventHandler.distinct((a, b) => a == b).listen((res) {
    //   if (res is fluwx.WeChatAuthResponse) {
    //     setState(() {
    //       _result = "state :${res.state} \n code:${res.code}";
    //     });
    //   }
    // });
    // super.initState();
    // fluwx.responseFromAuth.listen((data) {
    //   if (mounted) {
    //   }
    //   // 这里返回结果，errCode=1为微信用户授权成功的标志，其他看微信官方开发文档
    //   setState(() {
    //     _result2 = "initState ======   ${data.errCode}  --- ${data.code}";
    //     print(data);
    //     // int errCode = data.errCode;
    //     // if (errCode == Constant.WECHAT_SUCCESS) {
    //     //   String code = data.code;
    //     //   print('code  $code');
    //     //   getWeChatAccessToken(code);
    //     // }else {
    //     //   Util.showToast("${data.code}");
    //     // }
    //     print('aaaa ====== >   $_result2');
    //   });
    // });
  }

  // _initFluwx() async {
  //   var res = await registerWxApi(
  //       appId: "wxc9be39926ed03460",
  //       doOnAndroid: true,
  //       doOnIOS: true,
  //       universalLink: "https://your.univerallink.com/link/");
  //   print('初始化结果:$res');
  //   //是否安装了微信App
  //   var result = await isWeChatInstalled;
  //   print("is installed $result");
  // }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.deepOrangeAccent,
      body: SafeArea(
        child: Container(
            alignment: Alignment.centerLeft,
            child: Center(
                child: SingleChildScrollView(
              child: Container(
                margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30.0), 0,
                    ScreenUtil().setWidth(30.0), 0),
                height: ScreenUtil().setHeight(800.0),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.0)),
                child: Form(
                  key: registerFormKey,
                  child: Column(
                    children: <Widget>[
                      Padding(
                          padding: EdgeInsets.only(
                              top: ScreenUtil().setHeight(60.0))),
                      Container(
                        margin: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
                        child: TextFormField(
                          maxLines: 1,
                          maxLength: 11,
                          // autovalidate: _autovalidator,
                          keyboardType: TextInputType.phone,
                          validator: _validatorAccount,
                          decoration: InputDecoration(
                            icon: Icon(
                              Icons.account_circle,
                              color: Colors.deepOrangeAccent,
                              size: ScreenUtil().setWidth(60.0),
                            ),
                            hintText: Strings.ACCOUNT_HINT,
                            hintStyle: TextStyle(
                                color: Colors.grey,
                                fontSize: ScreenUtil().setSp(28.0)),
                            labelStyle: TextStyle(
                                color: Colors.black54,
                                fontSize: ScreenUtil().setSp(28.0)),
                            labelText: Strings.ACCOUNT,
                          ),
                          controller: _accountTextControl,
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
                        child: TextFormField(
                          maxLines: 1,
                          maxLength: 12,
                          obscureText: true,
                          // autovalidate: _autovalidator,
                          validator: __validatorPassWord,
                          decoration: InputDecoration(
                            icon: Icon(
                              MallIcon.PASS_WORD,
                              color: Colors.deepOrangeAccent,
                              size: ScreenUtil().setWidth(60.0),
                            ),
                            hintText: Strings.PASSWORD_HINT,
                            hintStyle: TextStyle(
                                color: Colors.grey,
                                fontSize: ScreenUtil().setSp(28.0)),
                            labelStyle: TextStyle(
                                color: Colors.black54,
                                fontSize: ScreenUtil().setSp(28.0)),
                            labelText: Strings.PASSWORD,
                          ),
                          controller: _passwordTextControl,
                        ),
                      ),
                      Container(
                          margin: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
                          child: SizedBox(
                            height: ScreenUtil().setHeight(80.0),
                            width: ScreenUtil().setWidth(600.0),
                            child: RaisedButton(
                              onPressed: _login,
                              color: Colors.deepOrangeAccent,
                              child: Text(
                                Strings.LOGIN,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontSize: ScreenUtil().setSp(28.0)),
                              ),
                            ),
                          )),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            margin: EdgeInsets.all(ScreenUtil().setWidth(20.0)),
                            alignment: Alignment.centerRight,
                            child: InkWell(
                              onTap: () => _register(),
                              child: Text(
                                Strings.NOW_REGISTER,
                                style: TextStyle(
                                    color: Colors.deepOrangeAccent,
                                    fontSize: ScreenUtil().setSp(24.0)),
                              ),
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.all(ScreenUtil().setWidth(20.0)),
                            alignment: Alignment.centerRight,
                            child: InkWell(
                              onTap: () => _weChatLogin(),
                              child: Text(
                                Strings.NOW_WX_LOGIN,
                                style: TextStyle(
                                    color: Colors.deepOrangeAccent,
                                    fontSize: ScreenUtil().setSp(24.0)),
                              ),
                            ),
                          )
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ))),
      ),
    );
  }

  _register() {
    NavigatorUtils.goRegister(context);
  }

  @override
  void dispose() {
    super.dispose();
    _result2 = null;
  }

// 某个按钮触发的操作
  Future<void> _weChatLogin() async {
    // await sendWeChatAuth(
    //   scope: "snsapi_userinfo"
    // );

    WeChatAuthResponse event = await WechatHelper().weChatAuth();
    if (event is WeChatAuthResponse) {
      WeChatAuthResponse respose = event;
      if(respose.code.isNotEmpty){
        WechatUserinfoModel wechatUserinfoModel = await getWechatUserinfo(respose.code);
        print(wechatUserinfoModel);
      }
    }



  }

  // openWeChatApp();

  String _validatorAccount(String value) {
    if (value == null || value.length < 11) {
      return Strings.ACCOUNT_RULE;
    }
    return null;
  }

  String __validatorPassWord(String value) {
    if (value == null || value.length < 6) {
      return Strings.PASSWORD_HINT;
    }
    return null;
  }

  _login() {
    if (registerFormKey.currentState.validate()) {
      registerFormKey.currentState.save();
      Map<String, dynamic> map = Map();
      map.putIfAbsent("username", () => _accountTextControl.text.toString());
      map.putIfAbsent("password", () => _passwordTextControl.text.toString());
      userService.login(map, (success) {
        print(success);
        userEntity = success;
        _saveUserInfo();
        _showToast(Strings.LOGIN_SUCESS);

        context.read<UserInfoModel>().updateInfo(userEntity);
        loginEventBus.fire(LoginEvent(true,
            url: userEntity.userInfo.avatarUrl,
            nickName: userEntity.userInfo.nickName));
        Navigator.pop(context);
      }, (onFail) {
        print(onFail);
        _showToast(onFail);
      });
    } else {
      setState(() {
        _autovalidator = true;
      });
    }
  }

  _showToast(message) {
    Fluttertoast.showToast(
        msg: message,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        backgroundColor: Colors.deepOrangeAccent,
        textColor: Colors.white,
        fontSize: ScreenUtil().setSp(28.0));
  }

  _saveUserInfo() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    SharedPreferencesUtils.token = userEntity.token;
    await sharedPreferences.setString(Strings.TOKEN, userEntity.token);
    await sharedPreferences.setString(
        Strings.HEAD_URL, userEntity.userInfo.avatarUrl);
    await sharedPreferences.setString(
        Strings.NICK_NAME, userEntity.userInfo.nickName);
  }
}
